package com.hlingsoft.kkh.action.admin;


import com.hlingsoft.kkh.entity.*;

import com.hlingsoft.kkh.entity.vo.EventsVo;
import com.hlingsoft.kkh.entity.vo.JsonVo;

import com.hlingsoft.kkh.entity.vo.PageVo;
import com.hlingsoft.kkh.util.HttpUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;


import javax.servlet.http.HttpServletRequest;

import java.util.ArrayList;

import java.util.List;

/**
 * Created by selbylei on 2015/5/23.
 */
@Controller
@RequestMapping("/admin/eventComments")
public class AdminEventCommentsAction extends AdminBaseAction {

    //搜索
    @RequestMapping(value = "/search.htm")
    public String search(
            @RequestParam(value = "eventCommentsId") Integer eventCommentsId,
            @RequestParam(value = "p", defaultValue = "1") int pageNum,
            HttpServletRequest request,
            ModelMap modelMap) {

        PageVo<EventComments> pageVo = new PageVo<EventComments>(pageNum);
        pageVo.setRows(10);
        pageVo.basePath = HttpUtils.getBasePath(request) + "/admin/eventComments/search.htm";

        List<EventComments> eventCommentsList = new ArrayList<EventComments>();
        if (eventCommentsId != null) {
            eventCommentsList.add(eventCommentsDao.getCommentsListByIdWithNone(eventCommentsId));
        }

        for (EventComments eventComments : eventCommentsList) {
            List<Image> imageList = imageDao.getImageByEventCommentsIdWithNone(eventComments.getId());
            eventComments.setImageList(imageList);
        }
        pageVo.setList(eventCommentsList);
        modelMap.put("pageVo", pageVo);
        pageVo.setCount(1);
        modelMap.put("p", pageNum);
        return "system/eventComments/list";
    }


    @RequestMapping(value = "/detail.htm")
    public String edit(
            @RequestParam(value = "eventCommentsId") Integer eventCommentsId,
            HttpServletRequest request,
            ModelMap modelMap) {
        EventComments eventComments = eventCommentsDao.getCommentsListByIdWithNone(eventCommentsId);
        List<Image> imageList = imageDao.getImageByEventCommentsIdWithNone(eventComments.getId());
        eventComments.setImageList(imageList);
        List<Reply> replyList = replyDao.getReplyByEventCommentsIdWithNone(eventCommentsId);
        eventComments.setReplyList(replyList);
        modelMap.put("eventComments", eventComments);
        return "system/eventComments/detail";
    }


    /**
     * 禁用帖子
     *
     * @param eventCommentsId
     * @param modelMap
     * @return
     */
    @RequestMapping(value = "/takeOff.json")
    @ResponseBody
    public JsonVo<String> takeOff(
            @RequestParam(value = "eventCommentsId") long eventCommentsId,
            HttpServletRequest request, ModelMap modelMap) {
        JsonVo<String> json = new JsonVo<String>();

        EventComments eventComments = eventCommentsDao.getCommentsListByIdWithNone(eventCommentsId);
        if (eventComments != null) {
            //删除回复
            List<Reply> replyList = replyDao.getReplyByEventCommentsIdWithNone(eventCommentsId);
            if (replyList.size() > 0) {
                for (Reply reply : replyList) {
                    reply.setAvailable(0);
                    replyDao.updateReply(reply);
                }
            }
            //删除图片
            List<Image> imageList = imageDao.getImageByEventCommentsIdWithNone(eventCommentsId);
            if (imageList.size()>0){
                for (Image image:imageList){
                    image.setAvailable(0);
                    imageDao.updateImage(image);
                }
            }

            List<Likes> likesList = likesDao.getLikesByEventCommentsIdWithNone(eventCommentsId);
        }


        json.setResult(true);
        return json;
    }

}
